package com.dreamer.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author zhoujie_root
 *
 * 传入规则和地址生成相应的防伪码
 */
public class CreateSecurityCodeUtils {

    /**
     * 生成防伪码，hym模式
     * @param rule
     * @param path
     * @return
     */
    public boolean createHYMCode(String rule,String path){
        String[] strCount = rule.split("-");
        Integer valueCount0 = Integer.valueOf(strCount[0]);
        Integer valueCount = Integer.valueOf(strCount[1]);
        Integer chu = 0;
        if((valueCount-valueCount0)%24 == 0){
            chu = (valueCount-valueCount0)/24;
        }else{
            chu = (valueCount-valueCount0 + 1)/24;
        }
        for(int x = 0;x<chu;x++){
            Integer bian = valueCount0;
            Integer hou = bian+23;
            //大码
            String boxCode = null;
            //小码
            String code = null;
            String str = "http://ht.haoyima.cn/hym/dmz/securityCode/search.html?boxs="+bian+"-"+hou;
            String[] strs = str.split("=");
            String[] strs1 = strs[1].split("-");
            Integer value2 = Integer.valueOf(strs1[0]);
            Integer value3 = Integer.valueOf(strs1[1]);
            for(int j = value2;j<=value3;j++){
                //大码
                boxCode = "http://ht.haoyima.cn/hym/dmz/securityCode/search.html?boxs="+value2+"-"+value3;
            }
            String str1 = "http://ht.haoyima.cn/hym/dmz/securityCode/search.html?code="+strs1[0];
            String[] strs2 = str1.split("=");
            for (int i = 0; i < strs2.length; i++) {
                if(i>0){
                    String temp1 = strs2[i];
                    Integer value1 = Integer.valueOf(temp1) -1;
                    int zhi1 = 24;
                    for (int k = 0; k < zhi1; k++){
                        value1++;
                        //小码
                        code = "http://ht.haoyima.cn/hym/dmz/securityCode/search.html?code="+value1;
                    }
                }
            }
            String[] strs3 = boxCode.split("=");
            String[] strs4 = strs3[1].split("-");
            Integer value4 = Integer.valueOf(strs4[0]);
            Integer value5 = Integer.valueOf(strs4[1]);
            String code1 = null;
            for(int i=value4;i<=value5;i++){
                code1 = "http://ht.haoyima.cn/hym/dmz/securityCode/search.html?code="+i;
                //大码小码放一起
                //System.out.println(boxCode + "  " + code1);
                try{
                    File file = new File(path);
                    file.createNewFile();
                    FileWriter fw = new FileWriter(file,true);
                    BufferedWriter bw = new BufferedWriter(fw);
                    bw.write(boxCode+"  "+code1+"\r\n");
                    bw.flush();
                    bw.close();
                    fw.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            for(int p=0;p<24;p++){
                valueCount0++;
            }
        }
        return true;
    }

    /**
     * 生成防伪码，jjl模式
     * @param rules
     * @param boxPath
     * @param path
     * @return
     * @throws IOException
     */
    public boolean createJJLCode(String[] rules,String boxPath,String path) throws IOException {
        String[] rule = rules[0].split("\\+");
        String[] rule1 = rules[1].split("\\+");
        String str1 = "http://server.jiajiani.cn/jjl/dmz/securityCode/search.html?boxCode="+rule[0];
        String[] strs1 = str1.split("=");
        int zhi1 = Integer.valueOf(rule[1]);
        for (int i = 0; i < strs1.length; i++) {
            if(i>0){
                String temp1 = strs1[i];
                Integer value1 = Integer.valueOf(temp1);
                String boxCodes="";
                for (int k = 0; k<zhi1;k++){
                    value1++;
                    boxCodes = "http://server.jiajiani.cn/jjl/dmz/securityCode/search.html?boxCode="+value1;
                    File file = new File(boxPath);
                    file.createNewFile();
                    FileWriter fw = new FileWriter(file,true);
                    BufferedWriter bw = new BufferedWriter(fw);
                    bw.write(boxCodes+"\r\n");
                    bw.flush();
                    bw.close();
                    fw.close();
                }
            }
        }
        String str = "http://server.jiajiani.cn/jjl/dmz/securityCode/search.html?code="+rule1[0];
        String[] strs = str.split("=");
        for (int i = 0; i < strs.length; i++) {
            if(i>0){
                String temp = strs[i];
                Integer value = Integer.valueOf(temp);
                int zhi = zhi1*20;
                String code1 = "";
                for (int k = 0; k<zhi;k++){
                    value++;
                    code1 = "http://server.jiajiani.cn/jjl/dmz/securityCode/search.html?code="+value;
                    File file = new File(path);
                    file.createNewFile();
                    FileWriter fileWriter = new FileWriter(file,true);
                    BufferedWriter bw = new BufferedWriter(fileWriter);
                    bw.write(code1+"\r\n");
                    bw.flush();
                    bw.close();
                    fileWriter.close();
                }
            }
        }
        return true;
    }

    /**
     * 生成防伪码，hzc模式
     * @param rules
     * @param boxPath
     * @param path
     * @return
     * @throws IOException
     */
    public boolean createHZCCode(String[] rules,String boxPath,String path) throws IOException {
        String[] rule = rules[0].split("\\+");
        String[] rule1 = rules[1].split("\\+");
        //String str1 = "http://ht.hzc365.com/hzc/dmz/securityCode/search.html?boxCode="+rule[0];
        String str1 = "http://ht.zmz365.com/zryk/dmz/securityCode/search.html?boxCode="+rule[0];
        String[] strs1 = str1.split("=");
        int zhi1 = Integer.valueOf(rule[1]);
        for (int i = 0; i < strs1.length; i++) {
            if(i>0){
                String temp1 = strs1[i];
                Integer value1 = Integer.valueOf(temp1);
                String boxCodes="";
                for (int k = 0; k<zhi1;k++){
                    value1++;
                    //boxCodes = "http://ht.hzc365.com/hzc/dmz/securityCode/search.html?boxCode="+value1;
                    boxCodes = "http://ht.zmz365.com/zryk/dmz/securityCode/search.html?boxCode="+value1;
                    File file = new File(boxPath);
                    file.createNewFile();
                    FileWriter fw = new FileWriter(file,true);
                    BufferedWriter bw = new BufferedWriter(fw);
                    bw.write(boxCodes+"\r\n");
                    bw.flush();
                    bw.close();
                    fw.close();
                }
            }
        }
        //String str = "http://ht.hzc365.com/hzc/dmz/securityCode/search.html?code="+rule1[0];
        String str = "http://ht.zmz365.com/zryk/dmz/securityCode/search.html?code="+rule1[0];
        String[] strs = str.split("=");
        for (int i = 0; i < strs.length; i++) {
            if(i>0){
                String temp = strs[i];
                Integer value = Integer.valueOf(temp);
                int zhi = zhi1*40;
                String code1 = "";
                for (int k = 0; k<zhi;k++){
                    value++;
                    code1 = "http://ht.zmz365.com/zryk/dmz/securityCode/search.html?code="+value;
                    File file = new File(path);
                    file.createNewFile();
                    FileWriter fileWriter = new FileWriter(file,true);
                    BufferedWriter bw = new BufferedWriter(fileWriter);
                    bw.write(code1+"\r\n");
                    bw.flush();
                    bw.close();
                    fileWriter.close();
                }
            }
        }
        return true;
    }


    public static void main(String[] args) {
        CreateSecurityCodeUtils c=new CreateSecurityCodeUtils();
        String path="C:\\Users\\Dream\\Desktop\\大小码\\大码.txt";
        String boxPath="C:\\Users\\Dream\\Desktop\\大小码\\小码.txt";
        String [] ruleStr=new String[]{"10000+275","100000+11000"};
        try{
            c.createHZCCode(ruleStr,path,boxPath);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
